package com.example.assign2_list;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Assign2_list extends ListActivity {

	private List<String> names = new ArrayList<String>();
	private Map<String, Class> activities = new HashMap<String, Class>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        /*	setup ListAdapter and add activities to list	*/
        SetActivities();
    	ListAdapter adapt = new ArrayAdapter<String>(this, R.layout.activity_assign2_list, names);
    	setListAdapter(adapt);
    	
    	/*	Config ListView	*/
    	ListView listView = getListView();
    	listView.setTextFilterEnabled(true);
    	listView.setOnItemClickListener(new ItemClick());
    }
    
    /*	If item clicked start activity */
    private class ItemClick implements OnItemClickListener {
    	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    		String activity_name = names.get(position);
    		Class activity_class = activities.get(activity_name);
    		
    		Intent intent = new Intent(Assign2_list.this, activity_class);
        	startActivity(intent);
    	}   	
    }
    
    private void AddActivity(String name, Class Activity){
    	names.add(name);
    	activities.put(name, Activity);
    }
    
    private void SetActivities(){
    	AddActivity("Alarm Clock", AlarmClock.class);
    	AddActivity("Visited Countries", MyCountries.class);
    	AddActivity("Mp3 player", Mp3Activity.class);
    }
}